<!-- 文章右侧边栏 -->

<script>
import { getBlogtype } from '@/api/blog';
import RightList from './RightList.vue';
export default {
  components: {
    RightList,
  },
  data() {
    return {
      data: null,
      isLoading: true,
    }
  },
  computed: {
    pagerData() {
      const categoryId = +this.$route.params.categoryId || -1 //-1查看全部
      const limit = +this.$route.query.limit || 10
      return {
        categoryId,
        limit
      }
    },
    list() {
      const total = this.data.reduce((a, b) => {
        return a + (+b.articleCount)
      }, 0)
      let res = [
        { id: -1, name: '全部', articleCount: total },
        ...this.data
      ]
      return res.map(it => {
        return {
          ...it,
          aside: `${it.articleCount}篇`,
          isSelected: it.id === this.pagerData.categoryId
        }
      })
    },
  },
  methods: {
    handSelect(item) {
      if (item.id === this.pagerData.categoryId) {
        return;
      }
      if (item.id === -1) {
        // 全部文章
        this.$router.push({
          path: '/blog',
          query: {
            page: 1,
            limit: this.pagerData.limit
          }
        });
      } else {
        // 分类文章
        this.$router.push({
          path: `/blog/cate/${item.id}`,
          query: {
            page: 1,
            limit: this.pagerData.limit
          }
        });
      }
    },
  },
  created() {
    getBlogtype().then((resp) => {
      this.data = resp.data
      this.isLoading = false
    })
  },

}
</script>



<template>
  <div class="blog-category-container" v-loading="isLoading">
    <h1>文章分类</h1>
    <RightList v-if="data" :list="list" @select="handSelect"></RightList>
  </div>
</template>



<style lang='less' scoped>
.blog-category-container {
  h1 {
    margin: 15px 10px;
    font-size: 18px;
  }
}
</style>